﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using Newtonsoft.Json.Linq;
using OnlineTestBLL;
using OnlineTestModel;

namespace OnlineTestUI
{
    public partial class paperview : System.Web.UI.Page
    {

        protected List<PaperQuestionEx> PqeList =new List<PaperQuestionEx>();
        protected List<StudentQuestions> SqList =new List<StudentQuestions>();
        protected StudentQuestions sq=new StudentQuestions();
        protected Papers p=new Papers();
        protected QuestionItemsBLL qib=new QuestionItemsBLL();
        protected Students s =new Students();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Session["student"]!=null)
            {
                s = (Students)Session["student"];
            }
            else
            {
                PageContext.Redirect("login.aspx");
            }
            if (!IsPostBack)
            {
                if (Request.QueryString["paperid"] != null && Request.QueryString["studentid"] != null)
                {
                    int paperid = Convert.ToInt32(Request.QueryString["paperid"]);
                    int studentid = Convert.ToInt32(Request.QueryString["studentid"]);
                    BindToData(paperid, studentid);
                }
            }
        }

        private void BindToData(int paperid, int studentid)
        {
            PapersBLL pb = new PapersBLL();
            StudentQuestionsBLL sqb = new StudentQuestionsBLL();
            p = pb.GetModel(paperid);
            QuestionsBLL qb = new QuestionsBLL();
            PqeList = qb.GetListByPaperId(" =" + paperid.ToString() + " order by bigquestion,smallquestion,id");
            SqList = sqb.GetModelList("paperid =" + paperid.ToString() + " and studentid =" + studentid.ToString());
        }

        protected StudentQuestions GetSq(int questionid)
        {
            Students student = (Students) Session["student"];
            return SqList.Single(s => s.QuestionId == questionid&&s.StudentId==student.Id);
        }

        protected List<QuestionItems> GetQi(int questionId)
        {
            return qib.GetModelList(" questionid =" + questionId);
        }

        protected void BtAdd_Click(object sender, EventArgs e)
        {
            StudentPapersBLL spb = new StudentPapersBLL();
            StudentQuestionsBLL sqb = new StudentQuestionsBLL();
            StudentPapers sp = new StudentPapers();
            QuestionsBLL qb = new QuestionsBLL();
            string paper = Request[@"paperscorelist"];
            JObject o = JObject.Parse(paper);
            JArray questionList = (JArray)o["question"];
            IList<StudentQuestions> sqList = questionList.ToObject<IList<StudentQuestions>>();
            using (TransactionScope ts = new TransactionScope())
            {
                try
                {
                    foreach (StudentQuestions sqs in sqList)
                    {
                        Questions q = qb.GetModel(sqs.QuestionId);
                        q.TotalCount = q.TotalCount + Convert.ToInt32(sqs.PaperId); //答题总数加1
                        q.CorrectCount = q.CorrectCount + Convert.ToInt32(sqs.Score);
                        q.TrueDifficult = (q.CorrectCount / q.TotalCount).ToString();
                        qb.Update(q);
                        sqb.UpdateScore(sqs);//每题评分
                    }
                    int paperId = Convert.ToInt32(Request.QueryString["paperid"]);
                    int studentId = Convert.ToInt32(Request.QueryString["studentid"]);
                    int totalScore = sqb.GetTotalScore(paperId, studentId);
                    spb.UpdateScore(paperId, studentId, totalScore);
                    ts.Complete();
                    Alert.Show("试卷评分成功");
                    PageContext.Redirect("paperlist.aspx", "_self");
                }
                catch (Exception)
                {
                    Alert.Show("出现错误");
                }
            }
        }
    }
}